# originally found on http://daringfireball.net/2007/03/new_text_files_contextual_menu

set myEmacsclient to "~/Documents/Code/emacs/nextstep/Emacs.app/Contents/MacOS/bin/emacsclient"



# create file in Finder

tell application "Finder"

try

set myFolder to (the target of the front window) as alias

on error

beep

end try

display dialog ¬

"New text file name:" default answer ".txt" buttons {"Cancel", "Create"} ¬

default button 2

set myFileName to text returned of result

if exists file myFileName of myFolder then

display alert ¬

"A file named ‘" & myFileName & "’ already exists in this folder." as informational

return

end if

set myPath to ((POSIX path of myFolder) & myFileName)

do shell script "touch " & quoted form of myPath

#open file in Emacs

display dialog ¬

"Open file with Emacs?" buttons {"Yes", "Cancel"} ¬

default button 1

if button returned of result is "Yes" then

try

tell application "System Events" to tell process "Emacs" to set frontmost to true

on error

tell application "/Users/suzume/Documents/Code/emacs/nextstep/Emacs.app"

activate

delay 1

end tell

end try

set selectedFile to myPath

set myCommand to myEmacsclient & " -n -e \"(find-file " & "\\\"" & selectedFile & "\\\"" & ")\""

do shell script myCommand

end if

end tell